From 79cb5b91063182bde3947975a0f16775d80f1149 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Hans=20J=C3=B8rgen=20Hoel?= Date: Tue, 19 May 2015 01:27:29 +0200 Subject: [PATCH] Make dl-snapshot.py avoid dl'ing existing files Check for existence of file in dl_path before fetching with curl. If file exists, compare hash with expected. Also wrap tarfile.open() in contextlib.closing() to support older python versions (<= 2.6). This fixes part of #1525. --- src/etc/dl-snapshot.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/etc/dl-snapshot.py b/src/etc/dl-snapshot.py index e5dfba772..c26297471 100644 --- a/src/etc/dl-snapshot.py +++ b/src/etc/dl-snapshot.py @@ -5,6 +5,7 @@ import subprocess import sys import tarfile import shutil +import contextlib with open('src/snapshots.txt') as f: lines = f.readlines() @@ -70,14 +71,22 @@ if not os.path.isdir('target/dl'): if os.path.isdir(dst): shutil.rmtree(dst) -ret = subprocess.call(["curl", "-o", dl_path, url]) -if ret != 0: - raise Exception("failed to fetch url") -h = hashlib.sha1(open(dl_path, 'rb').read()).hexdigest() -if h != hash: - raise Exception("failed to verify the checksum of the snapshot") +exists = False +if os.path.exists(dl_path): + h = hashlib.sha1(open(dl_path, 'rb').read()).hexdigest() + if h == hash: + print("file already present %s (%s)" % (dl_path, hash,)) + exists = True -with tarfile.open(dl_path) as tar: +if not exists: + ret = subprocess.call(["curl", "-o", dl_path, url]) + if ret != 0: + raise Exception("failed to fetch url") + h = hashlib.sha1(open(dl_path, 'rb').read()).hexdigest() + if h != hash: + raise Exception("failed to verify the checksum of the snapshot") + +with contextlib.closing(tarfile.open(dl_path)) as tar: for p in tar.getnames(): name = p.replace("cargo-nightly-" + triple + "/", "", 1) fp = os.path.join(dst, name) -- 2.30.2